package demo.practice.leetcode_base;

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}




public class Offer18 {

    public static void main(String[] args) {
        ListNode h= new ListNode(4);
        h.next= new ListNode(5);
        h.next.next= new ListNode(1);
        h.next.next.next= new ListNode(9);

        Offer18 offer18= new Offer18();
        offer18.deleteNode(h,5);
    }

    public ListNode deleteNode(ListNode head, int val) {

        if(head.val==val)
            return  head.next;

        ListNode current= head;
        while (current.next!=null && current.next.val!=val  ){
            current=current.next;
        }

        if(current.next!=null){
            current.next=current.next.next;

        }

        return head;
    }
}
